<?php

namespace App\HttpController\Rpc;

use EasySwoole\EasySwoole\ServerManager;
use EasySwoole\Rpc\NodeManager\FileManager;
use EasySwoole\Rpc\Rpc;

class  RpcServer
{
    public function test()
    {
        $rpcConfig = new \EasySwoole\Rpc\Config();
        //注册服务名称
        $rpcConfig->setServiceName('ser1');

//设置广播地址，可以多个地址
        $rpcConfig->getAutoFindConfig()->setAutoFindBroadcastAddress(['127.0.0.1:9600']);
//设置广播监听地址
        $rpcConfig->getAutoFindConfig()->setAutoFindListenAddress('127.0.0.1:9600');

        $rpcConfig->setNodeManager(FileManager::class);
        $rpc1 = new Rpc($rpcConfig);
        //注册响应方法
        $rpc1->registerAction('call1', function (\EasySwoole\Rpc\Request $request, \EasySwoole\Rpc\Response $response) {
            //获取请求参数

            //设置返回给客户端信息
            $response->setMessage('response');
        });


        //监听/广播 rpc 自定义进程对象
        $autoFindProcess = $rpc1->autoFindProcess('es_rpc_process_1');
        //增加自定义进程去监听/广播服务
        ServerManager::getInstance()->getSwooleServer()->addProcess($autoFindProcess->getProcess());
        //起一个子服务去运行rpc
        ServerManager::getInstance()->addServer('rpc1',9527);
        $rpc1->attachToServer( ServerManager::getInstance()->getSwooleServer('rpc1'));
    }
}